package edu.scau.DAO_mode.daoimp;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import javax.naming.spi.DirStateFactory.Result;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.entity.annotation.Table;

import edu.scau.DAO_mode.model.User;
import edu.scau.DAO_mode.util.DatabaseConnection;
import edu.scau.DAO_mode.dao.UserDao;



public class UserImp implements  UserDao{
	
     private Dao dao=null;
     private PreparedStatement state=null;
     private ResultSet rs=null;
     private DatabaseConnection db=new DatabaseConnection();
	//新增一条user表记录
    public UserImp() throws Exception{
    	
    	dao=db.getDao();
    }
	public boolean add(User user) throws Exception{
		dao.insert(user);
		return true;
		}
	//删除一条user表记录
	public boolean delete(User user) throws Exception {
		if (dao.delete(user)>0)
		return true;
		else return false;
		
	}
	//修改一条user表记录
	public boolean update(User user) throws Exception{
		if (dao.update(user)>0)
		return true;
		else return false;
		

	}
	//返回user表所有数据
	public List<User> findall() throws Exception {
		List<User> list=new ArrayList<User>();
        list=dao.query(User.class, null);
		return list;
		
	}
	@Override
	public boolean find(String username) throws Exception {
		User user=null;
		List<User> list=new ArrayList<User>();
        list=dao.query(User.class,Cnd.where("username", "=", username));
		if (list.size()>0) return true;
		              else return false;		
	}
	@Override
	public User returnUser(String username) throws Exception {
		List<User> list=new ArrayList<User>();
        list=dao.query(User.class,Cnd.where("username", "=", username));
		if (list.size()>0) return list.get(0);
		return null;
	}
	@Override
	public void rsClose() throws Exception {
		if (rs!=null) rs.close();
		
	}
	@Override
	public void stateClose() throws Exception {
		if (state!=null) state.close();
	}
    
	
}
